package com.ssy.lingxi.marketing.controller.coupon;

import com.ssy.lingxi.common.constant.marketing.MerchantCouponTypeEnum;
import com.ssy.lingxi.common.constant.member.AbilitySuitableMemberEnum;
import com.ssy.lingxi.common.controller.BaseController;
import com.ssy.lingxi.common.model.dto.UserLoginCacheDTO;
import com.ssy.lingxi.common.model.vo.SelectItemVO;
import com.ssy.lingxi.common.response.PageData;
import com.ssy.lingxi.common.response.Wrapper;
import com.ssy.lingxi.marketing.model.constant.MerchantCouponGetWayEnum;
import com.ssy.lingxi.marketing.model.constant.MerchantCouponStatusEnum;
import com.ssy.lingxi.marketing.model.vo.common.request.CommonAgreeRequest;
import com.ssy.lingxi.marketing.model.vo.common.request.CommonIdReq;
import com.ssy.lingxi.marketing.model.vo.common.request.CommonIdsRequest;
import com.ssy.lingxi.marketing.model.vo.coupon.request.*;
import com.ssy.lingxi.marketing.model.vo.coupon.response.*;
import com.ssy.lingxi.marketing.service.IMerchantCouponService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 *
 * @menu 商家优惠劵管理
 * @author yzc
 * @version 2.0.0
 * @date 2021/6/28
 */
@RestController
@RequestMapping("/marketing/coupon")
public class MerchantCouponController extends BaseController {

    @Resource
    private IMerchantCouponService merchantCouponService;

    // =============================状态列表=============================

    /**
     * 优惠券类型查询条件列表
     * @return 返回结果
     */
    @RequestMapping(value = "/typeList", method = RequestMethod.GET)
    public Wrapper<List<SelectItemVO>> listMerchantCouponType() {
        List<SelectItemVO> statusVOList = Stream.of(MerchantCouponTypeEnum.ZERO_DISCOUNT, MerchantCouponTypeEnum.COMMON, MerchantCouponTypeEnum.COMMODITY).map(e -> {
            SelectItemVO response = new SelectItemVO();
            response.setValue(e.getCode());
            response.setName(e.getMessage());
            return response;
        }).collect(Collectors.toList());
        return Wrapper.success(statusVOList);
    }

    /**
     * 优惠券领取方式查询条件列表
     * @return 返回结果
     */
    @RequestMapping(value = "/getWayList", method = RequestMethod.GET)
    public Wrapper<List<SelectItemVO>> listMerchantCouponGetWay() {
        List<SelectItemVO> statusVOList = Stream.of(MerchantCouponGetWayEnum.values()).map(e -> {
            SelectItemVO response = new SelectItemVO();
            response.setValue(e.getCode());
            response.setName(e.getMessage());
            return response;
        }).collect(Collectors.toList());
        return Wrapper.success(statusVOList);
    }

    /**
     * 优惠券状态查询条件列表
     * @return 返回结果
     */
    @RequestMapping(value = "statusList", method = RequestMethod.GET)
    public Wrapper<List<SelectItemVO>> listMerchantCouponStatus() {
        List<SelectItemVO> statusVOList = Stream.of(MerchantCouponStatusEnum.values()).map(e -> {
            SelectItemVO response = new SelectItemVO();
            response.setValue(e.getCode());
            response.setName(e.getMessage());
            return response;
        }).collect(Collectors.toList());
        return Wrapper.success(statusVOList);
    }

    /**
     * 1-新用户 2-老用户 3-新会员 4-老会员
     * @description 优惠券适用会员列表
     * @return 查询结果
     */
    @RequestMapping(value = "suitableMemberTypeList", method = RequestMethod.GET)
    public Wrapper<List<SelectItemVO>> listSuitableMemberTypeList() {
        List<SelectItemVO> statusVOList = Stream.of(AbilitySuitableMemberEnum.values()).map(e -> {
            SelectItemVO response = new SelectItemVO();
            response.setValue(e.getCode());
            response.setName(e.getMessage());
            return response;
        }).collect(Collectors.toList());
        return Wrapper.success(statusVOList);
    }

    /**
     * 商家优惠券分页列表查询条件
     * @return 返回结果
     */
    @RequestMapping(value = "/page/condition", method = RequestMethod.GET)
    public Wrapper<MerchantCouponConditionResponse> listMerchantCouponCondition() {
        MerchantCouponConditionResponse conditionResponse = new MerchantCouponConditionResponse();
        List<SelectItemVO> typeList = Stream.of(MerchantCouponTypeEnum.values()).map(e -> {
            SelectItemVO response = new SelectItemVO();
            response.setValue(e.getCode());
            response.setName(e.getMessage());
            return response;
        }).collect(Collectors.toList());
        conditionResponse.setTypeList(typeList);

        List<SelectItemVO> getWayList = Stream.of(MerchantCouponGetWayEnum.values()).map(e -> {
            SelectItemVO response = new SelectItemVO();
            response.setValue(e.getCode());
            response.setName(e.getMessage());
            return response;
        }).collect(Collectors.toList());
        conditionResponse.setGetWayList(getWayList);

        List<SelectItemVO> statusList = Stream.of(MerchantCouponStatusEnum.values()).map(e -> {
            SelectItemVO response = new SelectItemVO();
            response.setValue(e.getCode());
            response.setName(e.getMessage());
            return response;
        }).collect(Collectors.toList());
        conditionResponse.setStatusList(statusList);

        return Wrapper.success(conditionResponse);
    }

    // =============================商家优惠劵查询=============================

    /**
     * 商家优惠劵查询 - 分页列表
     * @return 返回结果
     */
    @RequestMapping(value = "/summary/page", method = RequestMethod.GET)
    public Wrapper<PageData<MerchantCouponSummaryPageResponse>> pageSummaryMerchantCoupon(@Valid MerchantCouponPageRequest request) {
        UserLoginCacheDTO loginUser = getSysUser();
        return merchantCouponService.pageSummaryMerchantCoupon(request, loginUser);
    }

    /**
     * 商家优惠劵查询 - 详情
     * @return 返回结果
     */
    @RequestMapping(value = "/summary/get", method = RequestMethod.GET)
    public Wrapper<MerchantCouponResponse> getSummaryMerchantCoupon(@Valid CommonIdReq request) {
        UserLoginCacheDTO loginUser = getSysUser();
        return merchantCouponService.getMerchantCoupon(request, loginUser);
    }

    /**
     * 商家优惠劵查询 - 修改
     * @param request 接口参数
     */
    @RequestMapping(value = "/waitAudit/modification", method = RequestMethod.POST)
    public Wrapper<Void> modificationMerchantCoupon(@RequestBody @Valid MerchantCouponModificationRequest request) {
        UserLoginCacheDTO loginUser = getSysUser();
        return merchantCouponService.modificationMerchantCoupon(request, loginUser);
    }


    /**
     * 商家优惠劵查询 - 终止
     * @param request 接口参数
     */
    @RequestMapping(value = "/summary/stop", method = RequestMethod.POST)
    public Wrapper<Void> stopMerchantCoupon(@RequestBody @Valid MerchantCouponOperationRequest request) {
        UserLoginCacheDTO loginUser = getSysUser();
        return merchantCouponService.stopMerchantCoupon(request, loginUser);
    }


    /**
     * 商家优惠劵查询 - 重启
     * @param request 接口参数
     */
    @RequestMapping(value = "/summary/restart", method = RequestMethod.POST)
    public Wrapper<Void> restartMerchantCoupon(@RequestBody @Valid MerchantCouponOperationRequest request) {
        UserLoginCacheDTO loginUser = getSysUser();
        return merchantCouponService.restartMerchantCoupon(request, loginUser);
    }


    /**
     * 商家优惠劵查询 - 取消
     * @param request 接口参数
     */
    @RequestMapping(value = "/summary/cancel", method = RequestMethod.POST)
    public Wrapper<Void> cancelMerchantCoupon(@RequestBody @Valid MerchantCouponOperationRequest request) {
        UserLoginCacheDTO loginUser = getSysUser();
        return merchantCouponService.cancelMerchantCoupon(request, loginUser);
    }


    // =============================待提交审核商家优惠劵=============================

    /**
     * 待提交审核商家优惠劵 - 分页列表
     * @return 返回结果
     */
    @RequestMapping(value = "/waitAudit/page", method = RequestMethod.GET)
    public Wrapper<PageData<MerchantCouponSubmitAuditPageResponse>> pageWaitAuditMerchantCoupon(@Valid MerchantCouponPageRequest request) {
        UserLoginCacheDTO loginUser = getSysUser();
        return merchantCouponService.pageSubmitAuditMerchantCoupon(request, loginUser);
    }

    /**
     * 待提交审核商家优惠劵 - 详情
     * @return 返回结果
     */
    @RequestMapping(value = "/waitAudit/get", method = RequestMethod.GET)
    public Wrapper<MerchantCouponResponse> getWaitAuditMerchantCoupon(@Valid CommonIdReq request) {
        UserLoginCacheDTO loginUser = getSysUser();
        return merchantCouponService.getMerchantCoupon(request, loginUser);
    }

    /**
     * 待提交审核商家优惠劵 - 新增
     * @param request 接口参数
     */
    @RequestMapping(value = "/waitAudit/add", method = RequestMethod.POST)
    public Wrapper<Void> addMerchantCoupon(@RequestBody @Valid MerchantCouponAddRequest request) {
        UserLoginCacheDTO loginUser = getSysUser();
        return merchantCouponService.addMerchantCoupon(request, loginUser);
    }

    /**
     * 待提交审核商家优惠劵 - 修改
     * @param request 接口参数
     */
    @RequestMapping(value = "/waitAudit/update", method = RequestMethod.POST)
    public Wrapper<Void> updateMerchantCoupon(@RequestBody @Valid MerchantCouponUpdateRequest request) {
        UserLoginCacheDTO loginUser = getSysUser();
        return merchantCouponService.updateMerchantCoupon(request, loginUser);
    }

    /**
     * 待提交审核商家优惠劵 - 删除
     * @param request 接口参数
     */
    @RequestMapping(value = "/waitAudit/delete", method = RequestMethod.POST)
    public Wrapper<Void> deleteMerchantCoupon(@RequestBody @Valid CommonIdsRequest request) {
        UserLoginCacheDTO loginUser = getSysUser();
        return merchantCouponService.deleteMerchantCoupon(request, loginUser);
    }

    /**
     * 待提交审核商家优惠劵 - 提交审核
     * @param request 接口参数
     */
    @RequestMapping(value = "/waitAudit/submitBatch", method = RequestMethod.POST)
    public Wrapper<Void> submitBatchAuditMerchantCoupon(@RequestBody @Valid CommonIdsRequest request) {
        UserLoginCacheDTO loginUser = getSysUser();
        return merchantCouponService.submitAuditMerchantCoupon(request, loginUser);
    }

    // =============================待审核商家优惠劵(一级)=============================

    /**
     * 待审核商家优惠劵(一级) - 分页列表
     * @return 返回结果
     */
    @RequestMapping(value = "/waitAuditOne/page", method = RequestMethod.GET)
    public Wrapper<PageData<MerchantCouponAuditPageResponse>> pageAuditOneMerchantCoupon(@Valid MerchantCouponPageRequest request) {
        UserLoginCacheDTO loginUser = getSysUser();
        return merchantCouponService.pageAuditOneMerchantCoupon(request, loginUser);
    }

    /**
     * 待审核商家优惠劵(一级) - 详情
     * @return 返回结果
     */
    @RequestMapping(value = "/waitAuditOne/get", method = RequestMethod.GET)
    public Wrapper<MerchantCouponResponse> getAuditOneMerchantCoupon(@Valid CommonIdReq request) {
        UserLoginCacheDTO loginUser = getSysUser();
        return merchantCouponService.getMerchantCoupon(request, loginUser);
    }


    /**
     * 待审核商家优惠劵(一级) - 提交审核
     * @param request 接口参数
     */
    @RequestMapping(value = "/waitAuditOne/audit", method = RequestMethod.POST)
    public Wrapper<Void> auditOneMerchantCoupon(@RequestBody @Valid CommonAgreeRequest request) {
        UserLoginCacheDTO loginUser = getSysUser();
        return merchantCouponService.auditOneMerchantCoupon(request, loginUser);
    }

    /**
     * 待审核商家优惠劵(一级) - 批量提交审核
     * @param request 接口参数
     */
    @RequestMapping(value = "/waitAuditOne/auditBatch", method = RequestMethod.POST)
    public Wrapper<Void> auditOneBatchMerchantCoupon(@RequestBody @Valid CommonIdsRequest request) {
        UserLoginCacheDTO loginUser = getSysUser();
        return merchantCouponService.batchAuditOneMerchantCoupon(request, loginUser);
    }

    // =============================待审核商家优惠劵(二级)=============================

    /**
     * 待审核商家优惠劵(二级) - 分页列表
     * @return 返回结果
     */
    @RequestMapping(value = "/waitAuditTwo/page", method = RequestMethod.GET)
    public Wrapper<PageData<MerchantCouponAuditPageResponse>> pageAuditTwoMerchantCoupon(@Valid MerchantCouponPageRequest request) {
        UserLoginCacheDTO loginUser = getSysUser();
        return merchantCouponService.pageAuditTwoMerchantCoupon(request, loginUser);
    }

    /**
     * 待审核商家优惠劵(二级) - 详情
     * @return 返回结果
     */
    @RequestMapping(value = "/waitAuditTwo/get", method = RequestMethod.GET)
    public Wrapper<MerchantCouponResponse> getAuditTwoMerchantCoupon(@Valid CommonIdReq request) {
        UserLoginCacheDTO loginUser = getSysUser();
        return merchantCouponService.getMerchantCoupon(request, loginUser);
    }


    /**
     * 待审核商家优惠劵(二级) - 审核
     * @param request 接口参数
     */
    @RequestMapping(value = "/waitAuditTwo/audit", method = RequestMethod.POST)
    public Wrapper<Void> auditTwoMerchantCoupon(@RequestBody @Valid CommonAgreeRequest request) {
        UserLoginCacheDTO loginUser = getSysUser();
        return merchantCouponService.auditTwoMerchantCoupon(request, loginUser);
    }

    /**
     * 待审核商家优惠劵(二级) - 批量审核
     * @param request 接口参数
     */
    @RequestMapping(value = "/waitAuditTwo/auditBatch", method = RequestMethod.POST)
    public Wrapper<Void> auditTwoBatchMerchantCoupon(@RequestBody @Valid CommonIdsRequest request) {
        UserLoginCacheDTO loginUser = getSysUser();
        return merchantCouponService.batchAuditTwoMerchantCoupon(request, loginUser);
    }

    // =============================待提交商家优惠劵=============================

    /**
     * 待提交商家优惠劵 - 分页列表
     * @return 返回结果
     */
    @RequestMapping(value = "/waitSubmit/page", method = RequestMethod.GET)
    public Wrapper<PageData<MerchantCouponSubmitPageResponse>> pageSubmitMerchantCoupon(@Valid MerchantCouponPageRequest request) {
        UserLoginCacheDTO loginUser = getSysUser();
        return merchantCouponService.pageSubmitMerchantCoupon(request, loginUser);
    }

    /**
     * 待提交商家优惠劵 - 详情
     * @return 返回结果
     */
    @RequestMapping(value = "/waitSubmit/get", method = RequestMethod.GET)
    public Wrapper<MerchantCouponResponse> getSubmitMerchantCoupon(@Valid CommonIdReq request) {
        UserLoginCacheDTO loginUser = getSysUser();
        return merchantCouponService.getMerchantCoupon(request, loginUser);
    }

    /**
     * 待提交商家优惠劵 - 批量提交
     * @param request 接口参数
     */
    @RequestMapping(value = "/waitSubmit/submitBatch", method = RequestMethod.POST)
    public Wrapper<Void> submitBatchMerchantCoupon(@RequestBody @Valid CommonIdsRequest request) {
        UserLoginCacheDTO loginUser = getSysUser();
        return merchantCouponService.submitMerchantCoupon(request, loginUser);
    }

    // =============================商家优惠劵执行=============================

    /**
     * 商家优惠劵执行 - 分页列表
     * @return 返回结果
     */
    @RequestMapping(value = "/waiteExecute/page", method = RequestMethod.GET)
    public Wrapper<PageData<MerchantCouponExecutePageResponse>> pageExecuteMerchantCoupon(@Valid MerchantCouponPageRequest request) {
        UserLoginCacheDTO loginUser = getSysUser();
        return merchantCouponService.pageExecuteMerchantCoupon(request, loginUser);
    }

    /**
     * 商家优惠劵执行 - 详情
     * @return 返回结果
     */
    @RequestMapping(value = "/waiteExecute/get", method = RequestMethod.GET)
    public Wrapper<MerchantCouponResponse> getExecuteMerchantCoupon(@Valid CommonIdReq request) {
        UserLoginCacheDTO loginUser = getSysUser();
        return merchantCouponService.getMerchantCoupon(request, loginUser);
    }

    /**
     * 商家优惠劵执行 - 发券
     * @param request 接口参数
     */
    @RequestMapping(value = "/waiteExecute/grant", method = RequestMethod.POST)
    public Wrapper<Void> grantMerchantCoupon(@RequestBody @Valid MerchantCouponGrantRequest request) {
        UserLoginCacheDTO loginUser = getSysUser();
        return merchantCouponService.grantMerchantCoupon(request, loginUser);
    }

    /**
     * 商家优惠劵执行 - 发券详情
     * @return 返回结果
     */
    @RequestMapping(value = "/waiteExecute/grant/get", method = RequestMethod.GET)
    public Wrapper<MerchantCouponGrantResponse> getGrantMerchantCoupon(@Valid CommonIdReq request) {
        UserLoginCacheDTO loginUser = getSysUser();
        return merchantCouponService.getGrantMerchantCoupon(request, loginUser);
    }

    /**
     * 商家优惠劵执行明细 - 查询条件
     * @return 返回结果
     */
    @RequestMapping(value = "/waiteExecute/detail/page/condition", method = RequestMethod.GET)
    public Wrapper<MerchantCouponDetailConditionResponse> getExecuteMerchantCouponDetailCondition(@Valid CommonIdReq request) {
        UserLoginCacheDTO loginUser = getSysUser();
        return merchantCouponService.getExecuteMerchantCouponDetailCondition(request);
    }

    /**
     * 商家优惠劵执行明细 - 分页列表
     * @return 返回结果
     */
    @RequestMapping(value = "/waiteExecute/detail/page", method = RequestMethod.GET)
    public Wrapper<PageData<MerchantCouponDetailPageResponse>> pageExecuteMerchantCouponDetail(@Valid MerchantCouponDetailPageRequest request) {
        UserLoginCacheDTO loginUser = getSysUser();
        return merchantCouponService.pageExecuteMerchantCouponDetail(request, loginUser);
    }
}
